package com.example.myprojectoffeixue;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;
import com.macrovideo.sdk.setting.AccountInfo;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class Switch extends Activity {
    private MyApplication m;
    IdeasExpandableListAdapter il;
    private Dialog dialog1;
    public boolean flag;
    List<SheBeiClass> groupList;
    List<List<SheBeiClass>> itemList ;
    boolean flag2=true;//添加延迟标志位

    Handler hand = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                if ("1007".equals(msg.obj)) {
                    if (dialog1.isShowing()) {
                        //ToastUtils.showToast(Switch.this,"我收到了开关反馈");
                        dialog1.dismiss();
                    }
                    il.notifyDataSetChanged();
                    Log.d("刷新","刷新");
                }
            } else if (msg.what == 10) {
                initData();
                il.notifyDataSetChanged();
            }  else if (msg.what == 55) {
                if (dialog1.isShowing()) {
                    dialog1.dismiss();
                    Toast.makeText(Switch.this, "请求超时", Toast.LENGTH_SHORT).show();
                }
            }
        };
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置列表适配器IdeasExpandableListAdapter
        setContentView(R.layout.activity_ideas_expandable_list_view);
        update();
        info();
    }
    Thread runner=new Thread() {

        @Override
        public void run() {
            for(int i=0;i<m.lsb.size();i++){
                Kuaiguanutil k = new Kuaiguanutil();
                k.cmd = "1007";
                k.ieee = m.lsb.get(i).ieee;
                k.port = m.lsb.get(i).port;
                k.mac = m.lsb.get(i).mac;
                k.status="02";
                String j = JSON.toJSONString(k);
                Log.d("cmdffffff",j);
                if(!(Gateway.MacSocke.containsKey(k.mac))){
                    if(m.flag3){
                    m.c.send(j);}
                }else{
                    String cmd= GatewayRequestUtil.createDeviceStatusStr(m.lsb.get(i).ieee, m.lsb.get(i).port,m.lsb.get(i).mac);
                    Log.d("我发的命令",cmd);
                    SendUtil.Mac=m.lsb.get(i).mac;
                    SendUtil.cmd=cmd;
                    SendUtil.hand.sendEmptyMessage(0);
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

            super.run();
        }
    };

    public void update(){
        m = (MyApplication) getApplication();
        m.hand = hand;
       runner.start();

    }

    private void initData() {

if(groupList==null){
    groupList=new ArrayList<SheBeiClass>();
    itemList=new ArrayList<List<SheBeiClass>>();
}else{
    groupList.clear();
    itemList.clear();
}

        if(m.list_sb!=null) {
            for (int i = 0; i < m.list_sb.size(); i++) {
                groupList.add(m.list_sb.get(i + "").get(0));
            }
            for (int j = 0; j < m.list_sb.size(); j++) {
                itemList.add(m.list_sb.get(j + ""));
            }
        }

    }
    public void info() {
        flag = true;
        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_mykaiguan);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        dialog1 = new Dialog(this, R.style.myDialogTheme);
        dialog1.setContentView(R.layout.progressdialog);
        ExpandableListView el = (ExpandableListView) findViewById(R.id.lt_expand);
        el.setGroupIndicator(null);
        el.setDivider(null);
        initData();
        il = new IdeasExpandableListAdapter(Switch.this, dialog1,groupList,itemList);
        el.setAdapter(il);
        for (int i = 0; i < il.getGroupCount(); i++) {
            el.expandGroup(i);
        }

       /* el.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {
            public boolean onItemLongClick(AdapterView<?> parent, View childView, int flatPos, long id)
            {
                if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
                {
                    long packedPos = ((ExpandableListView) parent).getExpandableListPosition(flatPos);
                    int groupPosition = ExpandableListView.getPackedPositionGroup(packedPos);
                    int childPosition = ExpandableListView.getPackedPositionChild(packedPos);
                    ToastUtils.showToast(getBaseContext(),"jjjjjjjjjjjjjjj");
                    return true;
                }else{

                    ToastUtils.showToast(getBaseContext(),"ccccccccccccccccccccc");
                    return true;

                }
            }

        });*/

    }

    @Override
    protected void onDestroy() {
        flag = false;
        super.onDestroy();
    }


}





